package cn.com.surker.lab.controller;

import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.base.system.service.SysLineService;
import cn.com.surker.common.AbstractController;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.lab.entity.LabExpendUpload;
import cn.com.surker.lab.service.LabExpendUploadService;
import cn.com.surker.spring.RestResultDto;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 技术上传配合表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-09-23
 */

@RestController
@RequestMapping("/lab")
public class LabController extends AbstractController {

	@Value("${erp.datasource.erp.db}")
	protected String erpDb;
	// lab_expend_upload
	@Resource
	private LabExpendUploadService expendUploadService;

	@Resource
	private SysLineService lineService;

	/**
	 * 查询任务单上传到工控机的生产线
	 *
	 * @param indentId
	 * @return
	 */
	@RequestMapping(value = "/indent/lines", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto indentLines(Long indentId) {
		if (indentId == null) {
			return RestResultDto.fail("未选择任务单");
		}
		List<LabExpendUpload> labExpendUploads = expendUploadService.queryByIndentId(erpDb,orgId, indentId, null);
		if (labExpendUploads == null || labExpendUploads.isEmpty()) {
			return RestResultDto.fail("该任务单没有上传配合比数据");
		}
		//	查询生产线
		List<Long> lineIds = labExpendUploads.stream().map(LabExpendUpload::getUploadLine).collect(Collectors.toList());
		QueryWrapper<SysLine> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		qw.in("id", lineIds);
		List<SysLine> lines = lineService.list(qw);
		if (lines == null || lines.isEmpty()) {
			return RestResultDto.fail("未查询到配类数据上传的生产线");
		}
		return RestResultDto.success(lines);
	}
}



